//
//  HJSettingVC.m
//  project
//
//  Created by jc on 2019/4/15.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJSettingVC.h"
#import "HJAlertView.h"
#import "SDImageCache.h"

@interface HJSettingVC ()
@property(nonatomic,strong) NSArray * titleArray;
@property(nonatomic,strong) NSArray * classArray;
@end

static NSString * const identifier = @"HJSettingCell";

@implementation HJSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle = @"设置";
}
-(void)configTableView {
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.rowHeight = 48;
    self.tableView.contentInset = UIEdgeInsetsMake(4, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self configOutLoginBtn];
}


-(NSArray *)titleArray {
    if (!_titleArray) {
        _titleArray = @[@[@"隐私设置",@"银行卡"],@[@"关于乐见",@"吐个槽"],@[@"清除缓存"]];
    }
    return _titleArray;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.titleArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.titleArray[section] count];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJSettingCell * settingCell =[tableView cellForRowAtIndexPath:indexPath];
    if (!settingCell) {
        settingCell = [[HJSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    [settingCell loadData:self.titleArray[indexPath.section][indexPath.row]];
    return settingCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([[self.titleArray[indexPath.section] lastObject] isEqualToString:@"清除缓存"]) {
        HJAlertView * alert = [HJAlertView alert];
        alert.content = @"是否清除缓存？";
        alert.cancelTitle = @"取消";
        alert.okTitle = @"清除缓存";
        [alert show];
        alert.block = ^{
            LKLog(@"确定按钮");
            [CommonTools clearFile];
            [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
            [[SDImageCache sharedImageCache] clearMemory];//清理内存
        };
    }else {
        [self ClasFrom:self.classArray[indexPath.section][indexPath.row]];
    }
}


-(NSArray *)classArray {
    if (!_classArray) {
        _classArray = @[@[@"HJPrivacyVC",@"HJBankListVC"],@[@"HJAboutLejianVC",@"HJTeasingVC"]];
    }
    return _classArray;
}

/**
 退出登录按钮
 */
-(void)configOutLoginBtn {
    UIButton * outLoginBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    outLoginBtn.frame = CGRectMake(10, self.view.bounds.size.height-42-Height_NavBar-60, self.view.bounds.size.width-20, 42);
    outLoginBtn.titleLabel.font = [UIFont systemFontOFSize15];
    [outLoginBtn setTitle:@"退出登录" forState:0];
    outLoginBtn.adjustsImageWhenHighlighted = NO;
    [outLoginBtn setBackgroundColor:[UIColor blackColor]];
//    @weakify(self)
    [[outLoginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
//        @strongify(self)
        [mUserDefaults setValue:@"0" forKey:@"isLogin"];
        [mUserDefaults synchronize];
        [HJUserManager isLogin];
        // 融云退出链接
        [[RCIMClient sharedRCIMClient] logout];
        
        NSString * url = [Api stringByAppendingString:outLogiApi];
        [HJHttpTool post:url params:nil success:^(HJResponeds *result) {
        } failure:^(id result) {
        }];
    }];
    [self.view setupShadowWithView:outLoginBtn];
    [self.view insertSubview:outLoginBtn aboveSubview:self.tableView];
}
@end



@implementation HJSettingCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initSubviews];
    }
    return self;
}


-(void)initSubviews {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor whiteColor];
    
//    UIView * bgView =[UIView new];
//    bgView.backgroundColor = [UIColor whiteColor];
////    [bgView rounded:UIRectCornerAllCorners radius:6];
//    self.bgView = bgView;
//    [self.contentView setupShadowWithView:bgView];
//    [self.contentView addSubview:bgView];
    
    UILabel * title = [UILabel new];
    title.font = [UIFont systemFontOFSize13];
    title.textColor = [UIColor colorWithHex:0x000000];
    title.textAlignment = NSTextAlignmentLeft;
    self.title = title;
    [self.contentView addSubview:title];
    
    UIImageView * rightImageView = [UIImageView new];
    [rightImageView setImage:setImage(@"ico_mine_right_row")];
    self.rightImageView = rightImageView;
    [self.contentView addSubview:rightImageView];
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self)
//    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
//        @strongify(self)
//        make.top.equalTo(@0);
//        make.left.equalTo(self.contentView).with.offset(0);
//        make.bottom.equalTo(self.contentView).with.inset(0);
//        make.right.equalTo(self.contentView).with.inset(0);
//    }];
//
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.right.equalTo(self.contentView).with.inset(17);
        make.centerY.mas_equalTo(self.contentView);
        make.size.mas_equalTo(CGSizeMake(self.rightImageView.image.size.width, self.rightImageView.image.size.height));
    }];

    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.contentView).with.offset(17);
        make.centerY.mas_equalTo(self.contentView);
        make.right.equalTo(self.rightImageView.mas_left).with.inset(12);
        make.height.mas_equalTo(30);
    }];
    
}

-(void)loadData:(NSString *)titleStr {
    self.title.text = titleStr;
}

@end
